<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use app\api\logic\Article as ArticleLogic;
use think\response\Json;

class Article extends Base
{
    /**
     * 查询列表
     * @throws
     */
    public function getList(): Json
    {
        //外部参数
        $params = $this->request->param();

        //验证规则
        $rule = [
            'page' => 'require|number',
        ];

        //错误信息
        $errMsg = [
            'page.require' => '页码page不得为空',
        ];

        //数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        //查询数据
        $list = app(ArticleLogic::class)->getList($params);

        //返回数据
        return $this->response($list);
    }

    /**
     * 详情内容
     * @throws BusinessException
     */
    public function detail(): Json
    {
        //外部参数
        $id = $this->request->param('id', 0);
        if (!$id) {
            throw new BusinessException('文章编号不得为空', 0);
        }

        //查询数据
        $info = app(ArticleLogic::class)->detail($id);

        //返回数据
        return $this->response($info);
    }
}
